###############################################
# ORDER OF THE SACRED MAGDALENE
# MAGDALENE SUCCESSION DECISION
#
# Normal feudal and tribal governments only.
# Nomads, republics, and theocracies are excluded.
#
# First establishment consumes 50% of current Grace.
# Establishing or repairing Magdalene succession also
# sets Carnal Realms Female Authority to Tier 4 so the
# realm law and Enatic succession remain synchronized.
# Once Magdalene Primogeniture exists, later rulers
# may reassert it without paying the founding cost again.
###############################################

society_decisions = {

    ggsm_establish_magdalene_succession = {

        potential = {
            society_member_of = ggsm_order_sacred_magdalene
            society_rank >= 4
            is_landed = yes

            OR = {
                government = feudal_government
                government = tribal_government
            }

            OR = {
                NOT = {
                    has_character_flag = ggsm_magdalene_succession_enacted
                }

                primary_title = {
                    NOT = {
                        has_law = cr_female_authority_law_4
                    }
                }

                any_demesne_title = {
                    OR = {
                        tier = COUNT
                        tier = DUKE
                        tier = KING
                        tier = EMPEROR
                    }
                    NOT = {
                        has_law = succ_magdalene_primogeniture
                    }
                }

                any_realm_lord = {
                    is_landed = yes
                    is_female = yes
                    religion = catholic
                    society_member_of = ggsm_order_sacred_magdalene

                    OR = {
                        government = feudal_government
                        government = tribal_government
                    }

                    OR = {
                        primary_title = {
                            NOT = {
                                has_law = cr_female_authority_law_4
                            }
                        }

                        any_demesne_title = {
                            OR = {
                                tier = COUNT
                                tier = DUKE
                                tier = KING
                                tier = EMPEROR
                            }
                            NOT = {
                                has_law = succ_magdalene_primogeniture
                            }
                        }
                    }
                }
            }
        }

        allow = {
            is_inaccessible_trigger = no
            prisoner = no
            in_command = no
        }

        effect = {
            # A realm with no established Magdalene succession law uses
            # the founding event and pays the one-time 50% Grace cost.
            if = {
                limit = {
                    NOT = {
                        has_character_flag = ggsm_magdalene_succession_enacted
                    }
                    NOT = {
                        any_demesne_title = {
                            has_law = succ_magdalene_primogeniture
                        }
                    }
                }

                long_character_event = {
                    id = ggsm.600
                }
            }

            # Once the law already exists, including after inheritance,
            # the same decision becomes a free manual repair pass.
            else = {
                # A successor who inherited the law receives the maintenance
                # flag and starts one annual maintenance chain. Existing rulers
                # already carrying the flag do not create duplicate chains.
                if = {
                    limit = {
                        NOT = {
                            has_character_flag = ggsm_magdalene_succession_enacted
                        }
                    }

                    set_character_flag = ggsm_magdalene_succession_enacted

                    hidden_tooltip = {
                        long_character_event = {
                            id = ggsm.601
                            days = 365
                        }
                    }
                }

                # Carnal Realms Female Authority is part of the reform.
                # Tier 4 is added directly so the Order decision does not
                # require the ruler to climb the secular law ladder first.
                primary_title = {
                    if = {
                        limit = {
                            NOT = {
                                has_law = cr_female_authority_law_4
                            }
                        }

                        add_law = {
                            law = cr_female_authority_law_4
                            cooldown = no
                            opinion_effect = no
                        }
                    }
                }

                any_demesne_title = {
                    limit = {
                        OR = {
                            tier = COUNT
                            tier = DUKE
                            tier = KING
                            tier = EMPEROR
                        }
                        NOT = {
                            has_law = succ_magdalene_primogeniture
                        }
                    }

                    add_law = {
                        law = succ_magdalene_primogeniture
                        cooldown = no
                        opinion_effect = no
                    }
                }

                any_realm_lord = {
                    limit = {
                        is_landed = yes
                        is_female = yes
                        religion = catholic
                        society_member_of = ggsm_order_sacred_magdalene

                        OR = {
                            government = feudal_government
                            government = tribal_government
                        }
                    }

                    # Keep each affected Magdalene ruler's CR gender law
                    # aligned with the Enatic succession being imposed.
                    primary_title = {
                        if = {
                            limit = {
                                NOT = {
                                    has_law = cr_female_authority_law_4
                                }
                            }

                            add_law = {
                                law = cr_female_authority_law_4
                                cooldown = no
                                opinion_effect = no
                            }
                        }
                    }

                    any_demesne_title = {
                        limit = {
                            OR = {
                                tier = COUNT
                                tier = DUKE
                                tier = KING
                                tier = EMPEROR
                            }
                            NOT = {
                                has_law = succ_magdalene_primogeniture
                            }
                        }

                        add_law = {
                            law = succ_magdalene_primogeniture
                            cooldown = no
                            opinion_effect = no
                        }
                    }
                }
            }
        }

        ai_will_do = {
            factor = 1

            # AI may establish the reform once. After that, the existing
            # annual ggsm.601 maintenance handles newly created titles,
            # preventing the AI from repeatedly clicking this decision.
            modifier = {
                factor = 0
                has_character_flag = ggsm_magdalene_succession_enacted
            }
        }
    }
}
